package simulatorGUI;
import everything.WorldMap;
import graphics.server.Boom;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;

import javax.swing.border.BevelBorder;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import markov.MDPState;
import markov.MDPStateTable;

/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class pnlMarkovView extends javax.swing.JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel pnlSetup;
	private JButton btnGo;
	private JLabel lblDirection;
	private JPanel pnlBoom;
	private JLabel lblIce;
	private JLabel lblFlags;
	private JTextField txtMoves;
	private JLabel lblMoves;
	private JPanel pnlIces;
	private JPanel pnlFlags;
	private JCheckBox[] chkFlags;
	private JCheckBox[] chkIces;
	private JComboBox cmdDirection;
	private MDPStateTable[] st;

	public pnlMarkovView(int[] flags, int[] ices, WorldMap m, MDPStateTable[] st, boolean remember) {
		super();
		this.st = st;
		initGUI(flags, ices, m, remember);
	}

	private void initGUI(final int[] flags, final int[] ices, final WorldMap m, final boolean remember) {
		try {
			BorderLayout thisLayout = new BorderLayout();
			this.setLayout(thisLayout);
			this.setPreferredSize(new java.awt.Dimension(522, 399));
			{
				pnlBoom = new JPanel();
				this.add(new JScrollPane(pnlBoom), BorderLayout.CENTER);
			}
			{
				pnlSetup = new JPanel();
				this.add(pnlSetup, BorderLayout.NORTH);
				pnlSetup.setPreferredSize(new java.awt.Dimension(522, 191));
				pnlSetup.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
				pnlSetup.setLayout(null);
				{
					btnGo = new JButton();
					pnlSetup.add(btnGo);
					btnGo.setText("Go!");
					btnGo.setBounds(442, 9, 49, 23);
					btnGo.addActionListener(new ActionListener(){
						@Override
						public void actionPerformed(ActionEvent e) {
							try{
								int movesLeft = Integer.parseInt(txtMoves.getText())-1;
								boolean[] stFlags = new boolean[flags.length];
								byte[] stIces = new byte[ices.length];
								for (int i=0; i<flags.length; i++){
									stFlags[i] = chkFlags[i].isSelected();
								}
								for (int i=0; i<ices.length; i++){
									if (chkIces[i].isEnabled()){
										if (chkIces[i].isSelected()){
											stIces[i] = MDPState.ICE;	
										} else {
											stIces[i] = MDPState.NO_ICE;
										}

									} else {
										stIces[i] = MDPState.UNKNOWN;
									}
								}
								byte dir = (byte)cmdDirection.getSelectedIndex();


								pnlBoom.removeAll();
								Boom bmain = new Boom();
								int Ysize = m.getHeight();
								int Xsize = m.getWidth();
								setSize(Xsize*46,Ysize*46);
								bmain.init(Xsize,Ysize);
								//bmain.setAgent(new SingleAgent("Walle", "Blue", "North", getCellNumber(board, x, y), 46, 46));

								for (int i=0; i<m.getHeight(); i++){
									for (int j=0; j<m.getWidth(); j++){
										if (m.brickStatus(i, j) == WorldMap.WALL){
											bmain.setWall(m.getCellNumber(i, j));
										} else {
											if (dir!=MDPState.NO_DIRECTION && (!m.isIce(i, j) || stIces[m.getIceId(i, j)]!=MDPState.ICE)){
												bmain.setNA(m.getCellNumber(i, j));
											} else {
												int stateId = new MDPState(stFlags, stIces, i, j , dir, m.getHeight(), m.getWidth()).getId();
												bmain.setMarkovAction(m.getCellNumber(i, j), st[movesLeft].getValue(stateId), st[movesLeft].getAction(stateId));
											}
										}
									}
								}

								pnlBoom.add(bmain);
								pnlBoom.repaint();
							}catch (Exception e1) {
								e1.printStackTrace();
								JOptionPane.showMessageDialog(null,"Exception - input data might be wrong", "Error",JOptionPane.ERROR_MESSAGE);
							}
						}
					});
				}
				{
					pnlFlags = new JPanel(new GridLayout(2, flags.length));
					pnlSetup.add(pnlFlags);
					pnlFlags.setBounds(2, 54, 503, 46);
					pnlFlags.setBorder(BorderFactory.createTitledBorder(""));
					chkFlags = new JCheckBox[flags.length];
					for(int i=0; i<flags.length; i++){
						pnlFlags.add(new JLabel(""+flags[i]));
					}
					for(int i=0; i<flags.length; i++){
						chkFlags[i] = new JCheckBox();
						pnlFlags.add(chkFlags[i]);
					}
				}
				{
					pnlIces = new JPanel(new GridLayout(2, ices.length));
					pnlSetup.add(pnlIces);
					pnlIces.setBorder(BorderFactory.createTitledBorder(""));
					pnlIces.setBounds(2, 131, 503, 54);
					chkIces = new JCheckBox[ices.length];
					for(int i=0; i<ices.length; i++){
						final int tmpi = i;
						JLabel tmp = new JLabel(""+ices[i]);
						tmp.addMouseListener(new MouseAdapter(){
							@Override
							public void mouseClicked(MouseEvent e) {
								super.mouseClicked(e);
								chkIces[tmpi].setEnabled(!chkIces[tmpi].isEnabled());
							}
						});
						pnlIces.add(tmp);
					}
					for(int i=0; i<chkIces.length; i++){
						chkIces[i] = new JCheckBox();
						pnlIces.add(chkIces[i]);
					}
				}
				{
					lblMoves = new JLabel();
					pnlSetup.add(lblMoves);
					lblMoves.setText("Moves left:");
					lblMoves.setBounds(7, 12, 74, 14);
					lblMoves.setFont(new java.awt.Font("Tahoma",1,12));
					if (!remember){
						lblMoves.setVisible(false);
					}
				}
				{
					txtMoves = new JTextField();
					pnlSetup.add(txtMoves);
					txtMoves.setBounds(85, 10, 47, 20);
					if (!remember){
						txtMoves.setText("2");
						txtMoves.setVisible(false);
					}
				}
				{
					lblFlags = new JLabel();
					pnlSetup.add(lblFlags);
					lblFlags.setText("Flags:");
					lblFlags.setBounds(2, 37, 41, 14);
					lblFlags.setFont(new java.awt.Font("Tahoma",1,12));
				}
				{
					lblIce = new JLabel();
					pnlSetup.add(lblIce);
					lblIce.setText("Ice:");
					lblIce.setBounds(2, 111, 33, 14);
					lblIce.setFont(new java.awt.Font("Tahoma",1,12));
				}
				{
					ComboBoxModel cmdDirectionModel = 
						new DefaultComboBoxModel(
								new String[] { "North", "South", "West", "East", "None" });
					cmdDirection = new JComboBox();
					pnlSetup.add(cmdDirection);
					cmdDirection.setModel(cmdDirectionModel);
					cmdDirection.setBounds(230, 10, 97, 20);
					cmdDirection.setSelectedIndex(4);
				}
				{
					lblDirection = new JLabel();
					pnlSetup.add(lblDirection);
					lblDirection.setText("Direction:");
					lblDirection.setBounds(164, 12, 62, 14);
					lblDirection.setFont(new java.awt.Font("Tahoma",1,12));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public int getCellNumber(int[][] board, int x, int y){
		return (y+x*board[0].length);
	}

	public void setStateTable(MDPStateTable[] st) {
		this.st = st;
	}
}
